<template>
	<view class="container" v-if="!$u.test.isEmpty(dataInfo)" :style="{background: 'url('+ $setting.imageUrl() +'shop_bg.png) no-repeat', backgroundSize: '100% 456rpx'}">
		
		<u-navbar title=" " :background="{backgroundColor: 'transparent'}" :border-bottom="false" back-icon-color="#ffffff">
			<u-search placeholder="搜索商品" color="#ffffff" placeholder-color="#ffffff" bg-color="rgba(255, 255, 255, 0.5)" :show-action="false" v-model="pageForm.search" @search="search"></u-search>
		</u-navbar>
		
		<!-- <view class="shop flex-align-center">
			<image class="shop-image" :src="dataInfo.detail.logos"></image>
			<view class="shop-con">
				<view class="shop-title flex-space-between">
					<view>{{dataInfo.detail.store_name}}</view>
					<view>店铺评分：{{dataInfo.detail.server_score}}</view>
				</view>
				<view class="shop-brand">主营品牌：{{dataInfo.detail.category_name}}</view>
				<view class="shop-sales flex-space-between">
					<view>销量：{{dataInfo.detail.product_sales}}件 丨 {{dataInfo.detail.fav_count}}人关注</view>
					<b-u-button-theme width="128rpx" height="52rpx" size="26rpx" @click="handleFavorite">{{dataInfo.detail.isfollow == 1 ? '取消关注' : '关注'}}</b-u-button-theme>
				</view>
			</view>
		</view> -->
		
		<view class="product bg-fff">
			<view class="tabs flex-align-center">
				<view class="tabs-list">
					<u-tabs :list="tabsList" :is-scroll="false" :current="tabsCurrent" :active-color="$setting.colorTheme()" @change="tabsChange"></u-tabs>
				</view>
				
				<image class="tabs-image" :src="$setting.imageUrl()+'screen.png'" v-show="screen == 1" @click="screen = 2"></image>
				<image class="tabs-image" :src="$setting.imageUrl()+'screen2.png'" v-show="screen == 2" @click="screen = 1"></image>
			</view>
			
			<scroll-view class="product-scroll" scroll-y="true" :style="{height: scrollHeight}" @scrolltolower="lower">
				<view class="product-con" v-show="screen == 1">
					<view class="product-item flex-align-center" v-for="(item,index) in productList" :key="index" @click="toDetail(item)">
						<image class="product-item-image" :src="item.product_image"></image>
						<view class="product-item-con flex-column-space-between">
							<view class="product-item-name u-line-2">{{item.product_name}}</view>
							<view class="flex-space-between">
								<view class="product-item-price flex-align-center">
									<view><text>￥</text>{{item.product_sku.product_price}}</view>
									<text>￥{{item.product_sku.line_price}}</text>
								</view>
								
								<view class="product-item-sales">交易{{item.product_sales}}笔</view>
							</view>
						</view>
					</view>
				</view>
				<view class="product-list flex-space-between flex-wrap" v-show="screen == 2">
					<view class="product-item bg-fff" v-for="(item,index) in productList" :key="index" @click="toDetail(item)">
						<image class="product-item-image" :src="item.product_image"></image>
						<view class="product-con">
							<view class="product-con-name u-line-2">{{item.product_name}}</view>
							<view class="flex-space-between">
								<view class="product-con-price"><text>￥</text>{{item.product_sku.product_price}}</view>
							</view>
							
							<view class="flex-space-between">
								<view class="product-item-old-price"><text>￥</text>{{item.product_sku.line_price}}</view>
								<view class="product-item-sales">交易{{item.product_sales}}笔</view>
							</view>
						</view>
					</view>
					
					<view class="product-item" v-if="productList.length % 2 == 1"></view>
				</view>
				
				<view v-if="productList.length > 0">
					<b-u-loadmore :status="loadmoreStatus"></b-u-loadmore>
				</view>
				<view v-else style="padding-top: 100rpx;">
					<u-empty text="暂无数据" mode="data"></u-empty>
				</view>
			</scroll-view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				scrollHeight: '',
				screen: 1,
				shop_supplier_id: 0,
				dataInfo: {},
				tabsList: [
					{
						id: 'all',
						name: "综合"
					},
					{
						id: 'sales',
						name: "销量"
					},
					{
						id: 'price',
						name: "价格(低)"
					},
					{
						id: 'price2',
						name: "价格(高)"
					},
				],
				tabsCurrent: 0,
				pageForm: {
					page: 1,
					list_rows: 20,
					sortType: 'all',
					sortPrice: 0,
					search: "",
					shop_supplier_id: '',
				},
				productList: [],
				loadmoreStatus: 'nomore',
			}
		},
		onLoad(options) {
			this.shop_supplier_id = options.shop_supplier_id;
			this.pageForm.shop_supplier_id = options.shop_supplier_id;
			this.getHttp();
		},
		methods: {
			toDetail(item){
				// console.log('item',item)
				if(item.categorydetail.type==1){
					this.jump('/pages/categoty/product/product_no_cart?id='+item.product_id)
				}else{
					this.jump('/pages/categoty/product/product?id='+item.product_id)
				}
			},
			lower() {
				if(this.loadmoreStatus == 'nomore') {
					return;
				}
				this.pageForm.page++;
				this.getProductLists();
			},
			handleFavorite() {
				uni.$u.api.postFavorite({
					pid: this.shop_supplier_id,
					type: 10
				}).then(res => {
					this.dataInfo.detail.isfollow == 1 ? this.dataInfo.detail.isfollow = 0 : this.dataInfo.detail.isfollow = 1;
					if (this.dataInfo.detail.isfollow == 0) {
						uni.showToast({
							title: "取消成功",
							icon: "none"
						})
					}
					if (this.dataInfo.detail.isfollow == 1) {
						uni.showToast({
							title: "关注成功",
							icon: "none"
						})
					}
				})
			},
			jump(url) {
				uni.navigateTo({
					url: url
				})
			},
			search() {
				this.pageForm.page = 1;
				this.productList = [];
				this.getProductLists();
			},
			tabsChange(e) {
				this.tabsCurrent = e;
				
				if(this.tabsList[e].id == 'price2') {
					this.pageForm.sortType = 'price';
					this.pageForm.sortPrice = 1;
				} else {
					this.pageForm.sortType = this.tabsList[e].id;
					this.pageForm.sortPrice = 0;
				}
				this.search();
			},
			getProductLists() {
				this.loadmoreStatus = 'loading';
				uni.$u.api.getProductLists(this.pageForm).then(res => {
					this.productList = this.productList.concat(res.list.data);
					this.loadmoreStatus = 'loadmore';
					if(this.productList.length  == res.list.total) {
						this.loadmoreStatus = 'nomore';
					}
				})
			},
			postSupplierInfo() {
				uni.$u.api.postSupplierInfo({
					shop_supplier_id: this.shop_supplier_id,
					visitcode: this.getVisitcode()
				}).then(res => {
					this.dataInfo = res;
					
					this.$nextTick(() => {
						const query = uni.createSelectorQuery();
						query.in(this).select('.product-scroll').boundingClientRect();
						query.exec((res) => {
							this.scrollHeight = uni.getSystemInfoSync().windowHeight - res[0].top - uni.upx2px(50) + 'px';
						})
					})
				})
			},
			getHttp() {
				this.postSupplierInfo();
				this.search();
			},
		},
	}
</script>

<style lang="scss" scoped>
	.container {
		
		/deep/.u-navbar-inner .u-search .u-input {
			background-color: transparent !important;
		}
		
		.shop {
			padding: 30rpx;
			
			.shop-image {
				width: 128rpx;
				height: 128rpx;
				border-radius: 160rpx;
			}
			
			.shop-con {
				flex: 1;
				margin-left: 16rpx;
			}
			
			.shop-title {
				font-size: 28rpx;
				color: #FFFFFF;
			}
			
			.shop-brand {
				margin-top: 16rpx;
				font-size: 24rpx;
				color: #FFFFFF;
			}
			
			.shop-sales {
				font-size: 24rpx;
				color: rgba(255, 255, 255, 0.8);
			}
		}
		
		.product {
			border-radius: 24rpx 24rpx 0 0;
			padding: 36rpx 30rpx;
			
			.tabs {
				
				.tabs-list {
					flex: 1;
				}
				
				.tabs-image {
					width: 44rpx;
					height: 44rpx;
				}
			}
			
			.product-con {
				padding-top: 30rpx;
				
				.product-item {
					height: 219rpx;
					margin-bottom: 30rpx;
					border-bottom: 1px solid #EBEBEB;
					padding-bottom: 30rpx;
					
					.product-item-image {
						width: 165rpx;
						height: 165rpx;
					}
					
					.product-item-con {
						margin-left: 30rpx;
						flex: 1;
						height: 100%;
						
						.product-item-name {
							font-size: 28rpx;
							color: #111111;
						}
						
						.product-item-price {
							
							view {
								font-size: 32rpx;
								color: $uni-color-theme;
								
								text {
									font-size: 22rpx;
									color: $uni-color-theme;
									text-decoration: none;
								}
							}
							
							text {
								font-size: 22rpx;
								text-decoration: line-through;
								color: #CCCCCC;
							}
						}
						
						.product-item-sales {
							font-size: 24rpx;
							color: #999999;
						}
					}
				}
			}
			
			.product-list {
				padding-bottom: 30rpx;
				margin-top: 28rpx;
				
				.product-item {
					width: 334rpx;
					border-radius: 16rpx;
					margin-bottom: 24rpx;
					overflow: hidden;
				}
				
				.product-item-image {
					width: 334rpx;
					height: 328rpx;
				}
				
				.product-con {
					padding: 12rpx 20rpx 18rpx;
					
					.product-con-name {
						font-size: 26rpx;
						color: #111111;
						font-weight: bold;
						margin-bottom: 14rpx;
						min-height: 68rpx;
					}
					
					.product-con-price {
						font-size: 36rpx;
						font-weight: bold;
						color: $uni-color-theme;
						
						text {
							font-size: 24rpx;
						}
					}
					
					.product-item-old-price {
						font-size: 22rpx;
						text-decoration: line-through;
						color: #CCCCCC;
					}
					
					.product-item-sales {
						font-size: 24rpx;
						color: #999999;
					}
				}
			}
		}
		
		
	}
</style>
